/*
 * CodeSaveCommand.java
 *
 * Created on 2006년 3월 29일 (수), 오후 4:11
 *
 * To change this template, choose Tools | Template Manager
 * and open the template in the editor.
 */

package com.qbooks.cms;

import java.util.*;




import javax.servlet.*;
import javax.servlet.http.*;

import org.json.simple.JSONObject;

import com.zll.framework.model.HashBox;
import com.zll.framework.model.HashList;
import com.zll.framework.servlet.Command;
import com.qbooks.common.CommonDAO;
import com.qbooks.common.CommonUtil;





/**
 *
 * @author eastar
 */
public class LoginCommand implements Command
{

    public String execute(HttpServletRequest  request, HttpServletResponse response)
    throws Exception
    {
		JSONObject json = new JSONObject();


    	HashBox box = new HashBox(request);



		CommonDAO dao 		= new CommonDAO();

		HashList list = dao.selectUserList(box.getParam());

		if (list.size() == 1) {
			String user_pw = list.get("user_pw", 0);
			if (box.get("user_pw").equals(user_pw)) {
				json.put("result", true);
				json.put("nextUrl", "/cms/main.jsp");

				HttpSession session = request.getSession();
				
				session.setAttribute("session.user_id", list.get("user_id",0));
				session.setAttribute("session.user_name", list.get("user_name",0));
				session.setAttribute("session.user_auth", list.get("user_auth",0));
				session.setAttribute("session.auth_name", list.get("auth_name",0));
				
				dao.updateLastLogin(box.getParam());

			} else {
				json.put("result", false);
				json.put("message", "비밀번호가 일치하지 않습니다.");
			}

		} else {
			json.put("result", false);
			json.put("message", "존재하지 않는 사용자입니다");
		}

		request.setAttribute("jsonData", json.toString());


    	return "/common/json_view.jsp";
    }
}
